<template>
  <div class="avatar">
    <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      text-color="balck"
      active-text-color="#ffd04b"
    >
      <el-submenu index="2">
        <template slot="title">
          <img src="../assets/nav-img/admin.svg" alt="" />
        </template>
        <el-menu-item>
          <h1>{{ status.username }}</h1>
          <h1>{{ name }}</h1>
        </el-menu-item>
        <el-menu-item index="5" @click="toregister()">注册</el-menu-item>
        <el-menu-item index="6" @click="exitClick()">退出登录</el-menu-item>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: 'avatar',
  data() {
    return {
      activeIndex: '1',
      status: {},
      name: '',
    };
  },
  created() {
    /* 方案二: cookie */
    /*页面挂载获取保存的cookie值，渲染到页面上*/
    let status = this.$cookies.get('username');
    console.log('status===', status);
    this.name = status;

    /* 方案一: localstorage */
    // var str = localStorage.getItem('status');
    // this.status = JSON.parse(str);
    // if (this.status.user_type == 'common') {
    //   this.common_show = true;
    // } else {
    //   this.common_show = false;
    // }
  },
  methods: {
    handleSelect(key, keyPath) {
      // console.log('handleSelect', key, keyPath);
    },
    exitClick() {
      ///*删除cookie*/
      this.$cookies.remove('status');
      this.$cookies.remove('username');
      this.$cookies.set('usertype');
      this.$router.push('/login');
      console.log('退出成功');
    },
    toregister() {
      this.$router.push('/register');
    },
  },
};
</script>
<style>
.avatar {
  width: 130px;
  position: absolute;
  top: 4px;
  right: -20px;
}
.avatar img {
  width: 40px;
  height: 40px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 5px 5px 4px rgba(0, 0, 0, 0.6);
}
.el-menu {
  width: 100px;
  background-color: rgba(0, 0, 0, 0);
}
.el-submenu__title {
  width: 50px;
  margin-left: 38px;
  padding-left: 5px;
}
.el-menu--horizontal {
  background-color: rgba(0, 0, 0, 0);
  width: 120px;
}
.el-menu.el-menu--horizontal {
  border-bottom: solid 0px #e6e6e6;
}
.el-menu--horizontal > .el-submenu .el-submenu__title:hover {
  background-color: rgba(0, 0, 0, 0);
}
.el-menu--horizontal .el-menu .el-menu-item {
  background-color: white;
  width: 120px;
  color: #317fcd;
}
</style>
